package com.panopset.gui.container;

import static com.panopset.Util.log;
import static com.panopset.Util.x;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JPanel;

import com.panopset.app.panopset.Application;

/**
 * File path persistent container.
 *
 * @author Karl Dinwiddie
 *
 */
public final class PanFile extends AbstractPanFile {

    /**
     * Test only.
     *
     * @param s
     *            Not used.
     */
    public static void main(final String... s) {
        new Application() {

            @Override
            protected JPanel createMainPanel() {
                JPanel p = new JPanel();
                p.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 2;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 1.0;
                p.add(new PanFile("test", "testing").getPanel(), gbc);
                p.setBackground(Color.YELLOW);
                return p;
            }
        }
        .setVisible(true);
    }

    /**
    *
    * @param borderTitle
    *            Border title.
    * @param toolTipText
    *            Tool tip text.
    */
   public PanFile(final String borderTitle, final String toolTipText) {
       this(borderTitle, toolTipText, "");
   }

   /**
    *
    * @param borderTitle
    *            Border title.
    * @param toolTipText
    *            Tool tip text.
    * @param defaultValue
    *            Default value.
    */
   public PanFile(final String borderTitle, final String toolTipText,
           final String defaultValue) {
       super(borderTitle, toolTipText, defaultValue);
   }

    @Override
    protected LayoutManager createLayout() {
        return new GridLayout(1, 1);
    }

    @Override
    protected boolean isValid(final String valueToCheck) {
        return true;
    }

    @Override
    protected String createPrompter() {
        return x("file");
    }

    @Override
    protected int getFileSelectionMode() {
        return JFileChooser.FILES_ONLY;
    }

    @Override
    protected void create() {
        try {
            FileWriter fw = new FileWriter(getFile());
            fw.write("");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            log(e);
        }
    }
}
